$(function () {
    $("#jqGrid").jqGrid({
        url:baseURL+"sys/merchant/list" ,
        datatype: "json",
        colModel: [
            { label: '报备id', name: 'reportId', width: 40,align:'center' , sortable: false},
            { label: 'SN', name: 'sn', width: 95,align:'center' , sortable: false},
            { label: '终端码', name: 'merchantCode', width: 90, align:'center' , sortable: false},
            { label: '商户名称', name: 'merchantName', width: 45,align:'center', sortable: false},
            { label: '联系方式', name: 'merPhone', width: 75,align:'center', sortable: false},
            { label: '机具名称', name: 'terminalName', width: 45,align:'center'},
            { label: '激活状态', name: 'isActivation', width:45, formatter:function(value, options, row){
                return value=='1'?"-":(value=='2'?"已激活":"禁用")},align:'center' , sortable: false},

            { label: '是否报备', name: 'isReport', width: 40,align:'center',formatter:function (value,options,row) {
                    return value==1?"否":"是";
                } , sortable: false},
			{ label: '是否线上', name: 'isOnline', width: 40,align:'center',formatter:function (value,option,row) {
                    return value==0?"否":"是";
                } , sortable: false},
            { label: '报备人', name: 'name', width: 75,align:'center',formatter:function (value,options,row) {
                    return value==null?"-":value;
                } , sortable: false},
            { label: '报备人手机', name: 'agentPhone', width: 75,align:'center',formatter:function (value,options,row) {
                    return value==null?"-":value;
                } , sortable: false},
            { label: '报备时间', name: 'createDate', width: 50,align:'center', sortable: false},
            { label: '交易额', name: 'totalAmount', width: 75,align:'center', sortable: false},
            /*{ label: '操作',name:'terminalId',formatter:function(value, options, row){

                    return '<span class = "btn btn-success" onclick = "toUpdate('+value+')"> 修改 </span>&nbsp;&nbsp;';
                },align:'center' , sortable: false}*/
        ],
		viewrecords: true,
        height: 375,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect:true,
        multiboxonly:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    bindType();

});


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            sn:null,
            merchantName:null,
            merchantCode:null,
            name:null,
            terminalType:0,
            reportDate:null
        },
        terminalTypes:[],
        showList: true,
        showUpdate:false,
        title:null,
        merchant:{
            reportId:null,
            merchantName:null,
            merchantCode:null,
            phone:null,
            sn:null
        }

    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean: function () {
            vm.showList = true;
            vm.q = {
                sn:null,
                merchantName:null,
                merchantCode:null,
                name:null,
                terminalType:0,
                reportDate:null
            };
            vm.merchant={
                reportId:null,
                    merchantName:null,
                    phone:null,
                    sn:null
            };
            $("#reportDate").val("");
            $("#jqGrid").jqGrid('setGridParam', {page: 1});
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            vm.showUpdate=false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');

            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'merchantCode': vm.q.merchantCode,
                    'terminalType': vm.q.terminalType,
                    'sn': vm.q.sn,
                    'merchantName':vm.q.merchantName,
                    "name": vm.q.name,
                    'reportDate': vm.q.reportDate
                },
                page: page
            }).trigger("reloadGrid");
            bindType();
        },
        deleteMer:function () {
            let rowid=getSelectedRow();
            if(rowid==null){
                return;
            }
            let row = $("#jqGrid").jqGrid('getRowData',rowid);
            if(row==null){
                return;
            }
            confirm("确定要删除该商户么？",function () {
                $.getJSON(baseURL + "sys/merchant/delete/" + row.reportId, function (r) {
                    vm.reload();
                })

            })

        },
        toUpdate:function () {
            let rowid=getSelectedRow();
            if(rowid==null){
                return;
            }
            let row = $("#jqGrid").jqGrid('getRowData',rowid);
            if(row==null){
                return;
            }
            $.get(baseURL+"sys/merchant/getById/"+row.reportId,r=>{
                console.log(r);
                vm.merchant=r.merchant;
            });
            vm.showList= false;
            vm.showUpdate=true;
            vm.title="商户修改"
        },
        updateMer:function () {
            $.ajax({
                url: baseURL + "sys/merchant/update",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(vm.merchant),
                success: function (r) {
                    if(r.code == 0){
                        alert('操作成功');
                        vm.reload();
                    }else{
                        alert(r.msg);
                    }
                }
            })
        }
    }
});



Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function bindType() {

        $.getJSON(baseURL + "sys/terminalType/list", function (r) {
            vm.$set(vm.terminalTypes, 0, {
                terminalType: 0,
                terminalName: "请选择类型"
            });
            for (var i = 0; i < r.page.length; i++) {
                vm.$set(vm.terminalTypes, (i + 1), r.page[i]);
            }

        })


}